package com.fit.networkShop.pages;

import com.fit.networkShop.asobject.PurchaseItemBasketAS;
import com.fit.networkShop.beans.PurchaseBean;
import com.fit.networkShop.interfaces.Customer;
import com.fit.networkShop.interfaces.Product;
import com.fit.networkShop.interfaces.Purchase;
import com.fit.networkShop.interfaces.PurchaseItem;
import com.fit.networkShop.interfaces.Seller;
import com.fit.networkShop.interfaces.User;
import com.fit.networkShop.service.Customers;
import com.fit.networkShop.service.Products;
import com.fit.networkShop.service.PurchaseItems;
import com.fit.networkShop.service.Purchases;
import com.fit.networkShop.service.Sellers;

import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Random;

public class EntryPurchase {
	
	@ApplicationState
	private PurchaseItemBasketAS purchaseItemBasketAS;
	
	private Product singleProduct;
	private PurchaseItem singlePurchaseItemFromBasket;
	
	@InjectPage
	private ViewPurchase viewPurchasePage;
	@InjectPage
	private ViewError viewErrorPage;
	
	@Inject
	private Products products;
	@Inject
	private Customers customers;
	@Inject
	private Sellers sellers;
	@Inject
	private Purchases purchases;
	@Inject
	private PurchaseItems purchasesItems;

	@ApplicationState
	private User user;
	private boolean userExists;
	public  User getUser() 	{
		return this.user;
	}

	public Collection<PurchaseItem> getPurchaseItemBasket() {
		return purchaseItemBasketAS.getPurchaseItemBasket().values();
	}
	public PurchaseItem getSinglePurchaseItemFromBasket() {
		return singlePurchaseItemFromBasket;
	}
	public void setSinglePurchaseItemFromBasket(PurchaseItem singlePurchaseItemFromBasket) {
		this.singlePurchaseItemFromBasket = singlePurchaseItemFromBasket;
	}

	public Collection<Product> getProducts() {
		return products.retrieve();
	}
	public Product getSingleProduct() {
		return singleProduct;
	}
	public void setSingleProduct(Product singleProduct) {
		this.singleProduct = singleProduct;
	}
	
	
	Object onActionFromRemoveProduct(String productName)
	{
		purchaseItemBasketAS.getPurchaseItemBasket().remove(productName);
		return null;
	}
	
	@OnEvent (value="submit", component="purchaseInputForm")
	Object onFromSubmit()
	{
		String pCustomer = getUser().getUserName();
		String pSeller = "SellerWebApp";

		Customer customer = null;
		Seller seller = null;
		
		try
		{
			customer = customers.retrieveSingleCustomer(pCustomer);
			seller = sellers.retrieveSingleSeller(pSeller);
		}
		catch(Exception ex)
		{
			viewErrorPage.setPassedMessage(ex.toString());
			return viewErrorPage;
		}

		if (customer == null)
		{
			viewErrorPage.setPassedMessage("Greska u prepoznavanju objekta Customer u Map-i");
			return viewErrorPage;			
		}
		if (seller == null)
		{
			viewErrorPage.setPassedMessage("Greska u prepoznavanju objekta Seller u Map-i");
			return viewErrorPage;			
		}

		Purchase pb = new PurchaseBean();
		pb.executePurchaseCreation(customer, seller, purchaseItemBasketAS, purchasesItems, purchases);
		
		purchaseItemBasketAS = null;
		return viewPurchasePage;
	}
}
